package com.example.tourstor;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;

public class GroupFragment extends ListFragment {

    private ArrayList<Quiz> listItems;
    private QuizAdapter adapter;
    Handler handler;

    EditText searchText ;
    Button searchBtn,myGroup,joinGroup;
    ListView listView;

    int G_id,U_id;
    String setOutTime,destination,cost,fuzeren,renshu,muqian;

    int UID;

    private static final String TAG = "GroupFragment";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.i(TAG, "onCreateView: ");
        return inflater.inflate(R.layout.frame_group, container);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        Log.i(TAG, "onActivityCreated: ");
        super.onActivityCreated(savedInstanceState);

//        Intent intent = getIntent();
        //UID=intent.getStringExtra("U_id");//获取上一页面传来的UID

        searchText = getActivity().findViewById(R.id.search_content);
        searchBtn = getActivity().findViewById(R.id.search_btn);
        myGroup = getActivity().findViewById(R.id.my_group);
        joinGroup = getActivity().findViewById(R.id.join_group);
        //listView=(ListView) findViewById(R.id.list);
        listView = getListView();

        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("U_id", Activity.MODE_PRIVATE);
        UID = sharedPreferences.getInt("U_id", 0);

        //点击跳转我的组队
        myGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.putExtra("U_id", UID);
                intent.setClass(getActivity(), CheckActivity.class);
                startActivity(intent);
            }
        });

        //点击跳转我加入的队伍
        joinGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.putExtra("U_id", UID);
                intent.setClass(getActivity(), joinGroupActivity.class);
                startActivity(intent);
            }
        });

        //点击搜索页面跳转至搜索结果
        searchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.putExtra("search_str", searchText.getText().toString());//将用户输入的内容传给下一个页面
                intent.putExtra("U_id", UID);
                intent.setClass(getActivity(), SearchActivity.class);
                startActivity(intent);
            }
        });


        /**
         * 最下方的list
         */
        //getLayoutInflater();//不确定←这句要不要......？？？？？？
        Thread t=new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (this){
                    //获取数据库数据，带回到主线程中
                    ArrayList<Quiz> retList = new ArrayList<Quiz>();
                    try {
                        java.sql.Connection cn = DBOpenHelper.getConn();
                        //从组团表获取所有信息
                        String sql = "select * from team";
                        PreparedStatement st = cn.prepareStatement(sql);
                        ResultSet rs = st.executeQuery();
                        if (rs != null) {
                            while (rs.next()) {
                                G_id = rs.getInt("G_id");
                                U_id = rs.getInt("U_id");
                                setOutTime = rs.getString("setOutTime");
                                destination = rs.getString("destination");
                                cost = rs.getString("cost");
                                renshu = rs.getString("number");
                                //利用组团表中的用户id再从用户表中获取用户名
                                String sql2 = "select * from User where U_id = ?";
                                PreparedStatement st2 = cn.prepareStatement(sql2);
                                st2.setInt(1,U_id);
                                ResultSet rs2 = st2.executeQuery();

                                if (rs2!=null){
                                    while(rs2.next()){
                                        fuzeren = rs2.getString("u_name");

                                        String sql3 = "select * from Gruop_User where G_id=?";
                                        PreparedStatement st3 = cn.prepareStatement(sql3);
                                        st3.setInt(1,G_id);
                                        ResultSet rs3 = st3.executeQuery();
                                        int mq=1;
                                        while(rs3.next()){
                                            mq++;
                                        }
                                        String mqrs = String.valueOf(mq);
                                        Quiz q=new Quiz(G_id,destination,setOutTime,cost,fuzeren,renshu,mqrs);
                                        retList.add(q);
                                    }
                                }

                            }
                        }
                        //DBOpenHelper.closeAll(cn, st);
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    Message msg = handler.obtainMessage(7);
                    msg.obj = retList;
                    handler.sendMessage(msg);
                }

            }
        });
        t.start();
        listItems =new ArrayList<Quiz>();
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg){
                if(msg.what==7){
                    listItems=(ArrayList<Quiz>) msg.obj;
                    if(listItems.size() == 0) {
                        Toast.makeText(getActivity(), "暂无组团信息", Toast.LENGTH_SHORT).show();
                    }else {
                        adapter = new QuizAdapter(getLayoutInflater(), listItems);
                        listView.setAdapter(adapter);
                    }
                }
                super.handleMessage(msg);
            }
        };

    }


}

